﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BP.DA;
using BP.En;
using BP.Sys;
using BP.Web.Controls;

namespace CCFlow.WF.Comm.RefFunc
{
	public partial class RefDtl : BP.Web.UC.UCBase3
	{
		#region 属性
		public new string EnsName
		{
			get
			{
				string str = this.Request.QueryString["EnsName"];
				if (str == null)
					return "BP.Edu.BTypes";
				return str;
			}
		}
		public string MainEnsName
		{
			get
			{
				return this.Request.QueryString["MainEnsName"];
			}
		}
		public string RefKey
		{
			get
			{
				return this.Request.QueryString["RefKey"];
			}
		}
		public string RefVal
		{
			get
			{
				string s = this.Request.QueryString["RefVal"];
				if (s != null)
					return s;

				s = this.Request.QueryString["PK"];
				if (s != null)
					return s;


				s = this.Request.QueryString["No"];
				if (s != null)
					return s;

				s = this.Request.QueryString["OID"];
				if (s != null)
					return s;

				s = this.Request.QueryString["MyPK"];
				if (s != null)
					return s;

				return s;
			}
		}
		public Entity HisEn
		{
			get
			{
				return this.HisEns.GetNewEntity;
			}
		}
		public Entities HisEns
		{
			get
			{
				Entities ens = BP.En.ClassFactory.GetEns(this.EnsName);
				return ens;
			}
		}
		public int PageSize
		{
			get
			{
				return BP.Sys.SystemConfig.PageSize;
			}
		}
		#endregion 属性

		protected void Page_Load(object sender, EventArgs e)
		{
			Entity en = this.HisEn;
			en.SetValByKey(this.RefKey, this.RefVal);
			UAC uac = en.HisUAC;

			if (uac.IsInsert || uac.IsUpdate)
			{
				this.ToolBar1.AddLinkBtn(NamesOfBtn.Save);
				//this.ToolBar1.AddLinkBtn(NamesOfBtn.SaveAndClose);
			}
			if (uac.IsDelete)
			{
				this.ToolBar1.AddLinkBtn(NamesOfBtn.Delete);
			}
			//this.ToolBar1.AddLinkBtn(NamesOfBtn.Excel, "导出Excel");

			if (this.ToolBar1.IsExit(NamesOfBtn.Save))
				this.ToolBar1.GetLinkBtnByID(NamesOfBtn.Save).Click += new EventHandler(ToolBar1_ButtonClick);

			//if (this.ToolBar1.IsExit(NamesOfBtn.SaveAndClose))
			//    this.ToolBar1.GetLinkBtnByID(NamesOfBtn.SaveAndClose).Click += new EventHandler(ToolBar1_ButtonClick);

			if (this.ToolBar1.IsExit(NamesOfBtn.Delete))
				this.ToolBar1.GetLinkBtnByID(NamesOfBtn.Delete).Click += new EventHandler(ToolBar1_ButtonClick);

			//if (this.ToolBar1.IsExit(NamesOfBtn.Excel))
			//    this.ToolBar1.GetLinkBtnByID(NamesOfBtn.Excel).Click += new EventHandler(ToolBar1_ButtonClick);

			this.Bind();
		}

		public void Bind()
		{
			#region 生成表头
			Entity en = this.HisEn;
			en.SetValByKey(this.RefKey, this.RefVal);

			Map map = this.HisEn.EnMap;
			Attrs attrs = map.Attrs;
			bool isFJ = false;
			if (attrs.Contains("MyFileName"))
				isFJ = true;
			//是否打开卡片.
			bool isOpenCard = false;
			if (attrs.Count > 12)
				isOpenCard = true;

			this.ucsys1.AddTable("class='Table' cellSpacing='1' cellPadding='1' border='1' style='width:100%'");
			this.ucsys1.AddTR();
			this.ucsys1.AddTDTitle();

			//“详细”列
			if (isOpenCard)
				this.ucsys1.AddTDTitle();

			//“选择全部”
			string str1 = "<INPUT id='checkedAll' onclick='SelectAll(this);' type='checkbox' name='checkedAll'>";
			this.ucsys1.AddTDTitle(str1);

			//实体字段
			foreach (Attr attr in attrs)
			{
				if (attr.UIVisible == false)
					continue;
				this.ucsys1.AddTDTitle(attr.Desc);
			}

			if (isFJ) //“附件”列
				this.ucsys1.AddTDTitle();
			this.ucsys1.AddTREnd();

			#endregion

			this.Page.Title = en.EnDesc;

			Entities dtls = this.HisEns;
			QueryObject qo = new QueryObject(dtls);
			qo.AddWhere(this.RefKey, this.RefVal);

			#region 生成翻页
			this.ucsys2.Clear();
			try
			{
				this.ucsys2.BindPageIdx(qo.GetCount(), this.PageSize, this.PageIdx, "Dtl.aspx?EnName=" + this.EnName + "&PK=" + this.RefVal + "&EnsName=" + this.EnsName + "&RefVal=" + this.RefVal + "&RefKey=" + this.RefKey + "&MainEnsName=" + this.MainEnsName);
				qo.DoQuery(en.PK, this.PageSize, this.PageIdx, false);
			}
			catch (Exception ex)
			{
				dtls.GetNewEntity.CheckPhysicsTable();

				this.Response.Write("@查询错误，有可能是表的字段有变化，请重重试让系统自动修复表字段，如果仍然不成功，就根据提示手动的修复字段。错误信息:" + ex.Message);
				//   this.Response.Redirect("Ens.aspx?EnsName=" + this.EnsName + "&RefPKVal=" + this.RefPKVal, true);
				return;
			}
			#endregion 生成翻页

			UAC uac = en.HisUAC;
			//if (uac.IsDelete == false)
			//    this.ToolBar1.GetLinkBtnByID(NamesOfBtn.Delete).Enabled = false; //禁用删除按钮

			if (uac.IsInsert) //默认的新建行
			{
				en.PKVal = "0";
				dtls.AddEntity(en);
			}

			DDL ddl = new DDL();
			CheckBox cb = new CheckBox();
			bool is1 = false;

			#region 生成数据
			int i = 0;
			foreach (Entity dtl in dtls)
			{
				i++;

				if (Equals(dtl.PKVal, "0") || dtl.PKVal.ToString() == "") //如果是新建行
				{
					this.ucsys1.AddTRSum();
					if (isOpenCard)
						this.ucsys1.AddTD("colspan=3", "<b>*</b>");
					else
						this.ucsys1.AddTD("colspan=2", "<b>*</b>");
				}
				else
				{
					//  is1 = this.ucsys1.AddTR(is1, "ondblclick=\"WinOpen( 'UIEn.aspx?EnsName=" + this.EnsName + "&PK=" + dtl.PKVal + "', 'cd' )\"");
					is1 = this.ucsys1.AddTR(is1);

					//  is1 = this.ucsys1.AddTR(is1);
					//行序号列
					this.ucsys1.AddTDIdx(i);
					//“详细”列
					if (isOpenCard)
						this.ucsys1.AddTD("<a href=\"javascript:WinOpen('/WF/Comm/En.htm?EnName=" + en.ToString() + "&PK=" + dtl.PKVal + "&closereload=1')\" >详细</a>");
					//“选中”列
					cb = new CheckBox();
					cb.ID = "CB_" + dtl.PKVal;
					this.ucsys1.AddTD(cb);
				}

				foreach (Attr attr in attrs)
				{
					if (attr.UIVisible == false)
						continue;

					if (attr.Key == "OID")
						continue;

					string val = dtl.GetValByKey(attr.Key).ToString();
					switch (attr.UIContralType)
					{
						case UIContralType.TB:
							TB tb = new TB();
							this.ucsys1.AddTD(tb);
							tb.LoadMapAttr(attr);
							tb.ID = "TB_" + attr.Key + "_" + dtl.PKVal;
							tb.Attributes["style"] = "width:" + attr.UIWidth + "px;border-width:0px;";
							switch (attr.MyDataType)
							{
								case DataType.AppMoney:
									tb.TextExtMoney = decimal.Parse(val);
									break;
								case DataType.AppDate:
									tb.Text = val.ToString();
									tb.ShowType = TBType.Date;
									if (attr.UIIsReadonly == false)
										tb.Attributes["onfocus"] = "WdatePicker();";
									break;
								case DataType.AppDateTime:
									tb.Text = val.ToString();
									tb.ShowType = TBType.DateTime;
									if (attr.UIIsReadonly == false)
										tb.Attributes["onfocus"] = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'});";
									break;
								default:
									tb.Text = val;
									break;
							}

							if (attr.IsNum && attr.IsFKorEnum == false)
							{
								if (tb.Enabled)
								{
									// OnKeyPress="javascript:return VirtyNum(this);"
									//  tb.Attributes["OnKeyDown"] = "javascript:return VirtyNum(this);";
									// tb.Attributes["onkeyup"] += "javascript:C" + dtl.PKVal + "();C" + attr.Key + "();";
									tb.Attributes["class"] = "TBNum";
								}
								else
								{
									//   tb.Attributes["onpropertychange"] += "C" + attr.Key + "();";
									tb.Attributes["class"] = "TBNumReadonly";
								}
							}
							break;
						case UIContralType.DDL:
							ddl = new DDL();
							ddl.ID = "DDL_" + attr.Key + "_" + dtl.PKVal;
							if (attr.UIIsReadonly == false)
							{
								ddl.Items.Add(new ListItem(dtl.GetValRefTextByKey(attr.Key), val));
								ddl.Enabled = false;
							}
							else
							{
								if (attr.IsEnum)
								{
									ddl.BindSysEnum(attr.UIBindKey);
								}
								else
								{
                                    if (attr.UIDDLShowType == DDLShowType.BindSQL)
                                    {
                                        string sql = attr.UIBindKey.Clone() as string;
                                        sql = sql.Replace("@WebUser.No", BP.Web.WebUser.No);
                                        sql = sql.Replace("@WebUser.Name", BP.Web.WebUser.Name);
                                        DataTable dt = BP.DA.DBAccess.RunSQLReturnTable(sql);
                                        ddl.BindSQL(sql, "No", "Name", val);  // (attr.HisFKEns, attr.UIRefKeyValue, attr.UIRefKeyText, true);

                                    }
                                    else
                                    {
                                        ddl.BindEntities(attr.HisFKEns, attr.UIRefKeyValue, attr.UIRefKeyText, true);
                                    }
								}
							}
							this.ucsys1.AddTD(ddl);
							ddl.SetSelectItem(val);
							break;
						case UIContralType.CheckBok:
							cb = new CheckBox();
							cb.ID = "CB_" + attr.Key + "_" + dtl.PKVal;
							cb.Text = attr.Desc;
							if (val == "1")
								cb.Checked = true;
							else
								cb.Checked = false;
							if (attr.UIIsReadonly)
								cb.Enabled = false;
							this.ucsys1.AddTD("nowarp=true", cb);
							break;
						default:
							break;
					}
				}

				if (isFJ == true)
				{
					string ext = dtl.GetValStrByKey("MyFileExt");
					if (ext != "")
						this.ucsys1.AddTD("<img src='../Images/FileType/" + ext + ".gif' border=0/>" + dtl.GetValStrByKey("MyFileName"));
				}

				this.ucsys1.AddTREnd();
			}

			#region 生成合计
			//this.ucsys1.AddTRSum();
			//this.ucsys1.AddTD("colspan=2", "合计");
			//foreach (Attr attr in attrs)
			//{
			//    if (attr.UIVisible == false)
			//        continue;
			//    if (attr.IsNum && attr.IsFKorEnum == false)
			//    {
			//        TB tb = new TB();
			//        tb.ID = "TB_" + attr.Key;
			//        tb.Text = attr.DefaultVal.ToString();
			//        tb.ShowType = attr.HisTBType;
			//        tb.ReadOnly = true;
			//        tb.Font.Bold = true;
			//        tb.BackColor = System.Drawing.Color.FromName("infobackground");

			//        switch (attr.MyDataType)
			//        {
			//            case DataType.AppRate:
			//            case DataType.AppMoney:
			//                tb.TextExtMoney = dtls.GetSumDecimalByKey(attr.Key);
			//                break;
			//            case DataType.AppInt:
			//                tb.TextExtInt = dtls.GetSumIntByKey(attr.Key);
			//                break;
			//            case DataType.AppFloat:
			//                tb.TextExtFloat = dtls.GetSumFloatByKey(attr.Key);
			//                break;
			//            default:
			//                break;
			//        }
			//        this.ucsys1.AddTD(tb);
			//    }
			//    else
			//    {
			//        this.ucsys1.AddTD();
			//    }
			//}
			//this.ucsys1.AddTD();
			//this.ucsys1.AddTREnd();
			#endregion 生成合计

			#endregion 生成数据
			this.ucsys1.AddTableEnd();
		}
		public void Save(bool isclose)
		{
			Entities dtls = BP.En.ClassFactory.GetEns(this.EnsName);
			Entity en = dtls.GetNewEntity;
			QueryObject qo = new QueryObject(dtls);
			qo.AddWhere(this.RefKey, this.RefVal);
			int num = qo.DoQuery(en.PK, this.PageSize, this.PageIdx, false);

			// qo.DoQuery(en.PK, 12, this.PageIdx, false);
			Map map = dtls.GetNewEntity.EnMap;

			int idx = 0;
			string msg = "";
			foreach (Entity dtl in dtls)
			{
				try
				{
					idx++;

					this.ucsys1.Copy(dtl, dtl.PKVal.ToString(), map);
					dtl.SetValByKey(this.RefKey, this.RefVal);
					dtl.Update();

				}
				catch (Exception ex)
				{
					msg += "@在保存(" + idx + ")行出现错误：" + ex.Message;
				}
			}

			en = this.ucsys1.Copy(en, "0", map);
			en.PKVal = "";
			bool isInsert = false;

			if (en.IsBlank == false)
			{
				if (en.IsNoEntity)
				{
					if (en.EnMap.IsAutoGenerNo)
						en.SetValByKey("No", en.GenerNewNoByKey("No"));
				}

				en.SetValByKey(this.RefKey, this.RefVal);
				try
				{
					en.Insert();
					isInsert = true;
				}
				catch (Exception ex)
				{
					msg += "@在插入新行时出现错误：" + ex.Message;
				}
			}

			if (msg.Length > 2)
			{
				throw new Exception(msg);
			}

			if (isclose)
			{
				this.WinClose();
				return;
			}

			int pageIdx = this.PageIdx;
			if (isInsert == true && num == this.PageSize)
				pageIdx++;

			this.Response.Redirect("Dtl.aspx?EnName=" + this.EnName + "&PK=" + this.RefVal + "&EnsName=" + this.EnsName + "&RefVal=" + this.RefVal + "&RefKey=" + this.RefKey + "&PageIdx=" + pageIdx + "&MainEnsName=" + this.MainEnsName, true);
		}

		public void ExpExcel()
		{
			//Entity mdtl = this.HisEn;
			//this.GenerLabel(this.Label1, mdtl.EnDesc);
			//this.Title = mdtl.Name;

			//GEDtls dtls = this.HisEns;
			//QueryObject qo = null;
			//qo = new QueryObject(dtls);
			//qo.DoQuery();

			////DataTable dt = dtls.ToDataTableDesc();
			//// this.GenerExcel(dtls.ToDataTableDesc(), mdtl.Name + ".xls");
			//this.GenerExcel_pri_Text(dtls.ToDataTableDesc(), mdtl.Name + "@" + WebUser.No + "@" + DataType.CurrentData + ".xls");

			//this.ExportDGToExcelV2(dtls, this.Title + ".xls");
			//dtls.GetNewEntity.CheckPhysicsTable();
			//this.Response.Redirect("Ens.aspx?EnsName=" + this.EnsName + "&RefPKVal=" + this.RefPKVal, true);
		}

		void ToolBar1_ButtonClick(object sender, EventArgs e)
		{
			var btn = sender as LinkBtn;
			try
			{
				switch (btn.ID)
				{
					case NamesOfBtn.New:
					case NamesOfBtn.Save:
					case NamesOfBtn.SaveAndNew:
						this.Save(false);
						break;
					case NamesOfBtn.SaveAndClose:
						this.Save(true);
						break;
					case NamesOfBtn.Delete:
						Entities dtls = BP.En.ClassFactory.GetEns(this.EnsName);
						QueryObject qo = new QueryObject(dtls);
						qo.AddWhere(this.RefKey, this.RefVal);
						qo.DoQuery("OID", this.PageSize, this.PageIdx, false);
						foreach (Entity dtl in dtls)
						{
							CheckBox cb = this.ucsys1.GetCBByID("CB_" + dtl.PKVal);
							if (cb == null)
								continue;

							if (cb.Checked)
								dtl.Delete();
						}
						this.ucsys1.Clear();
						this.Bind();
						break;
					case NamesOfBtn.Excel:
						this.ExpExcel();
						break;
					default:
						BP.Sys.PubClass.Alert("当前版本不支持此功能。");
						break;
				}
			}
			catch (Exception ex)
			{
				this.Alert(ex.Message);
			}
		}
		/// <summary>
		/// 生成列的计算
		/// </summary>
		/// <param name="pk"></param>
		/// <param name="attrs"></param>
		/// <param name="attr"></param>
		/// <returns></returns>
		public string GenerAutoFull(string pk, MapAttrs attrs, MapAttr attr)
		{
			return "";
			//string left = "\n  document.forms[0]." + this.ucsys1.GetTBByID("TB_" + attr.Key + "_" + pk).ClientID + ".value = ";
			//string right = attr.AutoFullDoc;
			//foreach (MapAttr mattr in attrs)
			//{
			//    string tbID = "TB_" + mattr.Key + "_" + pk;
			//    TB tb = this.ucsys1.GetTBByID(tbID);
			//    if (tb == null)
			//        continue;
			//    right = right.Replace("@" + mattr.Name, " parseFloat( document.forms[0]." + this.ucsys1.GetTBByID(tbID).ClientID + ".value.replace( ',' ,  '' ) ) ");
			//    right = right.Replace("@" + mattr.Key, " parseFloat( document.forms[0]." + this.ucsys1.GetTBByID(tbID).ClientID + ".value.replace( ',' ,  '' ) ) ");
			//}

			//string s = left + right;
			//s += "\t\n  document.forms[0]." + this.ucsys1.GetTBByID("TB_" + attr.Key + "_" + pk).ClientID + ".value= VirtyMoney(document.forms[0]." + this.ucsys1.GetTBByID("TB_" + attr.Key + "_" + pk).ClientID + ".value ) ;";
			//return s += " C" + attr.Key + "();";
		}
		public string GenerSum(MapAttr mattr, GEDtls dtls)
		{
			return "";
		}
	}
}